/*!
 * Pagination Widget 1.0 - 2015-06-12
 * http://www.neulion.com//
 */
$.widget( "neulion.pagination", {
	// default options
	options: {
		pageCount: null,
		pageIndex: null,
		leftRightTagCount: 1,
		currentNearbyTagCount: 1,
		givenShowCount: 0,  //0:不指定长度，>0:指定显示的个数（不包含左右按钮）
		preNodeValue: '<',
		nextNodeValue: '>',
		lineSpaceValue: '...'
	},
	_create: function () {
		this.element.addClass("pagination");
		this.updatePage(this.options.pageIndex, this.options.pageCount);
	},
	//  pageindex 当前页数，count 总页数
	updatePage: function (pageindex, count) {
		if (pageindex <= count && pageindex > 0 && count > 0) {
			// Clear element innerHTML
			this.element.empty();

			// Add Prev node
			this.element.append(this._createANode(this.options.preNodeValue, "prev"));

			// Add index node - givenShowCount > 0 ?   :
			var i;
			if (this.options.givenShowCount <= 0) { //0-Default
				// 左侧 1 -> pageIndex
				if (pageindex - 1 <= this.options.leftRightTagCount + this.options.currentNearbyTagCount) {
					for (i = 1; i < pageindex; i++) {
						this.element.append(this._createANode(i));
					}
				} else {
					for (i = 1; i <= this.options.leftRightTagCount; i++) {
						this.element.append(this._createANode(i));
					}
					if (this.options.lineSpaceValue.length > 0) {
						this.element.append(this._createSpanNode(this.options.lineSpaceValue));
					}
					for (i = pageindex - this.options.currentNearbyTagCount; i < pageindex; i++) {
						this.element.append(this._createANode(i));
					}
				}
				// 中间 pageindex status on
				this.element.append(this._createANode(pageindex, "on"));
				// 右侧 pageIndex->count
				if (count - pageindex <= this.options.currentNearbyTagCount + this.options.leftRightTagCount) {
					for (i = pageindex + 1; i <= count; i++) {
						this.element.append(this._createANode(i));
					}
				} else {
					for (i = pageindex + 1; i <= pageindex + this.options.currentNearbyTagCount; i++) {
						this.element.append(this._createANode(i));
					}
					if (this.options.lineSpaceValue.length > 0) {
						this.element.append(this._createSpanNode(this.options.lineSpaceValue));
					}
					for (i = count - this.options.leftRightTagCount + 1; i <= count; i++) {
						this.element.append(this._createANode(i));
					}
				}
			} else {
				var start = pageindex - parseInt(this.options.givenShowCount / 2);
				var end = start + this.options.givenShowCount - 1;
				if (this.options.givenShowCount >= count) {
					start = 1;
					end = count;
				} else if (start > 0 && (pageindex + parseInt(this.options.givenShowCount / 2) > count)) {
					start = count - this.options.givenShowCount + 1;
				} else if (start <= 0 && (pageindex + parseInt(this.options.givenShowCount / 2) <= count)) {
					end = this.options.givenShowCount;
				}
				start = (start > 0 ? start : 1 );
				end = (end - count > 0 ? count : end);

				for (i = start; i < pageindex; i++) {
					this.element.append(this._createANode(i));
				}
				this.element.append(this._createANode(pageindex, "on"));
				for (i = pageindex + 1; i <= end; i++) {
					this.element.append(this._createANode(i));
				}
			}

			// Add Next node
			this.element.append(this._createANode(this.options.nextNodeValue, "next"));

			// Control  "unclick" status of pre and next
			if (1 == pageindex) {
				//this.element.children(":first-child").addClass("unclick");
				this.element.children().first().addClass("unclick");
			} else if (count == pageindex) {
				this.element.children(":last-child").addClass("unclick");
			}
		}
	},
	//  Create Anchor Node function
	_createANode: function (content, classValue) {
		var aVar = $("<a></a>");
		aVar.attr('href', '#');
		aVar.html(content);
		//aVar.append(this._createSpanNode(null,'page-item'));
		//aVar.append(this._createSpanNode(content));
		aVar.addClass(classValue || "");
		var that = this;
		aVar.click(function () {
			that._anchorClick(aVar);
		});
		return aVar;
	},
	//  Create Span Node function for "..."
	_createSpanNode: function (content, classValue) {
		var spanVar = $("<span></span>");
		spanVar.html(content);
		spanVar.addClass(classValue || "");
		return spanVar;
	},
	// Anchor onClick Method
	_anchorClick: function (currentNode) {
		var anchorCount = this.options.pageCount;
		var anchorLastIndex = parseInt(this.element.children(".on").html());
		var anchorValue = currentNode.html();
		var anchorIndex = -1;
		if (this.element.children(":first-child").is(currentNode)) {
			anchorIndex = anchorLastIndex - 1;
		} else if (this.element.children(":last-child").is(currentNode)) {
			anchorIndex = anchorLastIndex + 1;
		} else {
			anchorIndex = parseInt(anchorValue);
		}
		if (anchorIndex > 0 && this.options.pageIndex != anchorIndex && anchorIndex < anchorCount + 1) {
			this.options.pageIndex = anchorIndex;
			this._trigger("pagechange", null, {pageIndex: anchorIndex});

			// Needed only for no trigger function to refresh pagination.
			this.updatePage(anchorIndex, anchorCount);
		}
	},

	// events bound via _on are removed automatically
	// revert other modifications here
	_destroy: function () {
		// remove generated elements
		this.element
			.removeClass("pagination")
			.text("");
	},

	// _setOptions is called with a hash of all options that are changing
	// always refresh when changing options
	_setOptions: function () {
		// _super and _superApply handle keeping the right this-context
		this._superApply(arguments);
//				this._refresh();
	},

	// _setOption is called for each individual option that is changing
	_setOption: function (key, value) {
		// prevent invalid color values
		this._super(key, value);
	}
});
